package org.glorylight.hacks.social.service;

import java.util.Map;

import javax.annotation.Resource;

import org.glorylight.hacks.social.data.MailMe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class MailService {
	private final static Logger log = LoggerFactory.getLogger(MailService.class);

	@Autowired
	private MailSender mailSender;
	
	@Resource(name="template")
	private Map<String, String> template;
	
	@Async
	public void sendMail(MailMe mailme) throws Exception {
		log.debug("trying to send mail");
		SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom(mailme.getSender());
		message.setTo(mailme.getRecipient());
		message.setSubject(mailme.getSubject());
		message.setText(String.format(template.get(mailme.getText()), mailme.getTextParam()));
		mailSender.send(message);
	}
}
